package mr.ali.videoapp.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import mr.ali.videoapp.portal.mapper.CollectionCllectMapper;
import mr.ali.videoapp.portal.model.Collection;
import mr.ali.videoapp.portal.model.CollectionCllect;
import mr.ali.videoapp.portal.service.ICollectionCllectService;
import mr.ali.videoapp.portal.service.ICollectionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Mr-ali
 * @since 2025-06-30
 */
@Service
public class CollectionCllectServiceImpl extends ServiceImpl<CollectionCllectMapper, CollectionCllect> implements ICollectionCllectService {

    @Resource
    private CollectionCllectMapper collectionCllectMapper;

    @Resource
    private ICollectionService collectionService;

    @Override
    public List<CollectionCllect> findAll(Integer userId, Integer videoId) {
        QueryWrapper<CollectionCllect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        if (videoId != null) {
            queryWrapper.eq("video_id", videoId);
        }
        queryWrapper.orderByDesc("gmt_create");

        List<CollectionCllect> tags = collectionCllectMapper.selectList(queryWrapper);
        for (CollectionCllect video : tags) {
            List<Collection> videos = collectionService.videoTagsById(video.getVideoId());
            video.setCollection(videos);
        }
        return tags;
    }

    @Override
    public Integer delete(Integer userId, Integer videoId) {
        QueryWrapper<CollectionCllect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("video_id", videoId);
        return collectionCllectMapper.delete(queryWrapper);
    }

    @Override
    public Boolean exists(Integer userId, Integer videoId) {
        QueryWrapper<CollectionCllect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("video_id", videoId);
        return collectionCllectMapper.selectCount(queryWrapper) > 0;
    }

    @Override
    public Integer insert(Integer userId, Integer videoId) {
        QueryWrapper<CollectionCllect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("video_id", videoId);
        Long row = collectionCllectMapper.selectCount(queryWrapper);
        if (row < 1) {
            CollectionCllect collectionCllect = new CollectionCllect();
            collectionCllect.setUserId(userId);
            collectionCllect.setVideoId(videoId);
            collectionCllect.setGmtCreate(System.currentTimeMillis());
            collectionCllectMapper.insert(collectionCllect);
            return 1;
        } else if (row > 0) {
            collectionCllectMapper.delete(queryWrapper);
            return 2;
        }
        return 0;
    }
}
